﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using Autofac;
using Damany.Cameras;
using Damany.PC.Domain;

namespace RemoteImaging.VideoSource
{
    class VideoSourceModule : Autofac.Module
    {
        protected override void Load(Autofac.ContainerBuilder builder)
        {
            IVideoSource source = new NullVideoSource();
            var cameraInfo = Damany.RemoteImaging.Common.ConfigurationManager.GetDefault().GetCameras().FirstOrDefault();
            if (cameraInfo != null)
            {
                source = CreateVideoSourceFrom(cameraInfo);
            }

            builder.RegisterInstance(source).SingleInstance().As<IVideoSource>().OnActivated(c => c.Instance.Start());
        }


        private IVideoSource CreateVideoSourceFrom(Damany.PC.Domain.CameraInfo cameraInfo)
        {
            IVideoSource _source = null;

            switch (cameraInfo.Provider)
            {
                case CameraProvider.LocalDirectory:
                    try
                    {
                        var dir = new Damany.Cameras.DirectoryFilesCamera(cameraInfo.Location.LocalPath, "*.jpg");
                        dir.FrameIntervalMs = cameraInfo.Interval;
                        dir.Repeat = true;
                        _source = dir;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("创建摄像头发生错误，请重新配置选项。\r\n===============\r\n" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        _source = new NullVideoSource();
                    }

                    break;

                case CameraProvider.Sanyo:
                    var sanyo = new JPEGExtendStream(cameraInfo.Location.ToString());
                    sanyo.Login = cameraInfo.LoginUserName ?? "guest";
                    sanyo.Password = cameraInfo.LoginPassword ?? "guest";
                    sanyo.FrameInterval = cameraInfo.Interval;

                    sanyo.RequireCookie = cameraInfo.Provider == CameraProvider.Sanyo;
                    _source = sanyo;
                    break;
                case CameraProvider.AipStar:

                    var aironix = new FoKoShVideoSourceAdapter(IntPtr.Zero);
                    aironix.Camera.UserName = cameraInfo.LoginUserName ?? "system";
                    aironix.Camera.Password = cameraInfo.LoginPassword ?? "system";
                    aironix.Camera.Ip = cameraInfo.Location.Host.ToString(CultureInfo.InvariantCulture);
                    aironix.Camera.Port = 6002;
                    aironix.FrameInterval = cameraInfo.Interval;
                    aironix.Camera.StreamId = 0;
                    _source = aironix;
                    break;

                case CameraProvider.CyNet:
                    var ip = IPAddress.Parse(cameraInfo.Location.Host);
                    var port = 8888;
                    var endpoint = new IPEndPoint(ip, port);
                    var cyCamera = new CyNetCamera(endpoint);
                    _source = cyCamera;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }

            return _source;
        }

    }
}
